<template>
    <div style="padding: 1rem">
        <el-row :span="24">
            <el-col class="toolbar" style="padding-bottom: 0">
                <el-form :inline="true">
                    <el-form-item>
                        <el-button icon="el-icon-plus" type="primary" @click="addMenu()">添加</el-button>
                        <!--                        <el-button size="small" icon="el-icon-back" type="primary" @click="back()">返回</el-button>-->
                        <el-form-item>
                            <el-button type="danger" v-show="checkedAll" icon="el-icon-delete" @click="allDelete()">批量删除
                            </el-button>
                        </el-form-item>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
        <el-table
            v-loading="listLoading"
            :data="list"
            align="center"
            class="orderList"
            size="small"
            style="margin-top: 1rem"
            tooltip-effect="dark"
            @selection-change="handleSelectionChange"
            border
        >
            <el-table-column
                type="selection"
                width="55"
            />
            <el-table-column prop="proclamationContent" label="公告内容" width=""/>
            <!--            <el-table-column prop="createTime" label="创建时间" width=""/>-->
            <!--            <el-table-column prop="updateTime" label="修改时间" width=""/>-->
            <el-table-column label="操作" width="" align="center">
                <template slot-scope="scope">
                    <el-tooltip content="修改" placement="top">
                        <el-button
                            size="mini"
                            type="primary"
                            icon="el-icon-edit"
                            circle
                            @click="handleUpdate(scope.$index,scope.row)"
                        />
                    </el-tooltip>
                    <el-tooltip content="启用" placement="top" v-if="scope.row.type == 2">
                        <el-button
                            size="mini"
                            type="success"
                            icon="el-icon-check"
                            circle
                            @click="handleShow(scope.$index,scope.row)"
                        />
                    </el-tooltip>
                    <el-tooltip content="禁用" placement="top" v-if="scope.row.type == 1">
                        <el-button
                            size="mini"
                            type="danger"
                            icon="el-icon-circle-close"
                            circle
                            @click="handleStop(scope.$index,scope.row)"
                        />
                    </el-tooltip>
                    <el-tooltip content="删除" placement="top">
                        <el-button
                            size="mini"
                            type="danger"
                            icon="el-icon-delete"
                            circle
                            @click="handleDelete(scope.$index,scope.row)"
                        />
                    </el-tooltip>
                </template>
            </el-table-column>
        </el-table>
        <el-col :span="24" class="toolbar" style="margin-bottom: 2rem">
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :page-sizes="[10,20, 50, 100]"
                :page-size=size
                layout="total, sizes, prev, pager, next, jumper"
                :total=total>
            </el-pagination>
        </el-col>
        <el-dialog
            width="40%"
            :close-on-click-modal="false"
            :title="title"
            :visible.sync="dialogFormVisible"
            @close="closeDialog('ruleForm')"
        >
            <el-form ref="ruleForm" :model="ruleForm" :rules="rules" class="demo-ruleForm">
                <el-form-item label="公告信息" prop="proclamationContent" :label-width="formLabelWidth">
                    <el-input
                        type="textarea"
                        autosize
                        placeholder="请输入内容"
                        :autosize="{ minRows: 2}"
                        v-model="ruleForm.proclamationContent">
                    </el-input>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
        <el-button @click="editForm('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </span>
        </el-dialog>
    </div>
</template>

<script>
    import {ggList, insetData, ggEdit, ggDeleta, orderDelete, ggAll} from '../../api/modeList'
    import NProgress from "nprogress";
    import {formatDateTime} from "../../utils";
    import moment from "moment";
    import $ from "jquery";
    import { eventBus } from '../../main'
    export default {
        name: "noteManagement",
        data() {
            return {
                listLoading: false,
                list: [],
                ruleForm: {
                    proclamationContent: '',
                },
                rules: {
                    proclamationContent: [
                        {required: true, message: '请输入公告信息', trigger: 'blur'},
                    ],
                },
                title: '',
                dialogFormVisible: false,
                formLabelWidth: '100px',
                total: 0,
                size: 10,
                pageNo: 1,
                multipleSelection: [],
                checkedAll: false,  //批量删除标识
            }
        },
        mounted() {
            this.getList()
        },
        methods: {
            search() {
                //搜索 根据当前后台的逻辑搜索的时候永远将页码置于1
                this.pageNo = 1;
                this.getList();
            },
            getList() {
                let params = {
                    currPage: this.pageNo,
                    pageSize: this.size,
                };
                NProgress.start();
                this.listLoading = true;
                ggList(params).then((res) => {
                    if (res.data.code === 0) {
                        const data = res.data.data;
                        this.list = data.records;
                        this.size = data.size;
                        this.total = data.total;
                        const len = this.list.length;
                        if (len === 0 && this.pageNo > 1) {
                            this.pageNo -= 1;
                            this.getList()
                        }
                        this.list.map(val => {
                            switch (val.type) {
                                case 1:
                                    break;
                                case 2:
                                    break;
                            }
                            if (val.proclamationCreatetime != null) {
                                let createTime = new Date(val.proclamationCreatetime);
                                val.createTime = formatDateTime(createTime);
                            }
                            if (val.proclamationUpdatetime != null) {
                                let updateTime = new Date(val.proclamationUpdatetime);
                                val.updateTime = formatDateTime(updateTime)
                            }
                        })
                    } else {
                        this.$message({
                            type: 'error',
                            message: res.data.msg,
                            duration: 1000
                        });
                    }
                    this.listLoading = false;
                    NProgress.done();
                })
            },
            addMenu() {
                this.title = '添加公告';
                this.dialogFormVisible = true;
                this.ruleForm.proclamationContent = ''
            },
            handleShow(index, row) {
                let params = {
                    proclamationId: row.proclamationId,
                    type: 1,
                };
                ggEdit(params).then((res) => {
                    if (res.data.code === 0) {
                        this.$message({
                            type: 'success',
                            message: res.data.msg,
                            duration: 1000
                        });
                        this.getList();
                        this.messageSister();
                    } else {
                        this.$message({
                            type: 'error',
                            message: res.data.msg,
                            duration: 1000
                        });
                        this.getList();
                    }
                })
            },
            messageSister() {
                eventBus.$emit('brotherSaid', '启用公告!!!')
            },
            messageSister2() {
                eventBus.$emit('brotherSaid2', '停用公告!!!')
            },
            handleStop(index, row) {
                let params = {
                    proclamationId: row.proclamationId,
                    type: 2,
                };
                ggEdit(params).then((res) => {
                    if (res.data.code === 0) {
                        this.$message({
                            type: 'success',
                            message: res.data.msg,
                            duration: 1000
                        });
                        this.getList();
                        this.messageSister2();
                    } else {
                        this.$message({
                            type: 'error',
                            message: res.data.msg,
                            duration: 1000
                        });
                        this.getList();
                        this.messageSister2();
                    }
                })
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if (this.title === '添加公告') {
                            let param = {
                                proclamationContent: this.ruleForm.proclamationContent
                            };
                            insetData(param).then((res) => {
                                if (res.data.code === 0) {
                                    this.$message({
                                        type: 'success',
                                        message: '添加成功',
                                        duration: 1000
                                    });
                                    this.getList();
                                    this.messageSister();
                                    this.dialogFormVisible = false;
                                } else {
                                    this.$message({
                                        type: 'error',
                                        message: res.data.msg,
                                        duration: 1000
                                    });
                                }
                            })
                        } else if (this.title === '修改公告') {
                            let param = {
                                proclamationId: this.ruleForm.proclamationId,
                                proclamationContent: this.ruleForm.proclamationContent,
                                type: this.ruleForm.type,
                            };
                            ggEdit(param).then((res) => {
                                if (res.data.code === 0) {
                                    this.$message({
                                        type: 'success',
                                        message: '修改成功',
                                        duration: 1000
                                    });
                                    this.getList();
                                    this.messageSister();
                                    this.dialogFormVisible = false;
                                } else {
                                    this.$message({
                                        type: 'error',
                                        message: res.data.msg,
                                        duration: 1000
                                    });
                                }
                            })
                        }

                    }
                })
            },
            editForm() {
                this.dialogFormVisible = false;
            },
            handleUpdate(index, row) {
                this.title = '修改公告';
                this.ruleForm = {
                    proclamationId: row.proclamationId,
                    proclamationContent: row.proclamationContent,
                    type: row.type,
                };
                this.dialogFormVisible = true
            },
            handleDelete(index, row) { // 删除公告
                this.$confirm('此操作将永久删除该公告, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    const  dataIds = []
                    dataIds.push(row.proclamationId)
                    ggDeleta(dataIds).then((res) => {
                        if (res.data.code === 0) {
                            this.$message({
                                type: 'success',
                                message: '删除成功!',
                                duration: 1000
                            });

                        } else {
                            this.$message({
                                type: 'error',
                                message: res.data.msg,
                                duration: 1000
                            })
                        }
                        this.getList()
                        this.messageSister();
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    })
                })
            },
            allDelete() { // 批量删除
                this.$confirm('此操作将永久删除当前选中公告, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    const dataIds = []
                    this.multipleSelection.map(val => {
                        dataIds.push(val.proclamationId)
                    });
                    ggDeleta(dataIds).then((res) => {
                        if (res.data.code === 0) {
                            this.$message({
                                type: 'success',
                                message: res.data.msg,
                                duration: 1000
                            });
                            this.getList()
                            this.messageSister();
                        } else {
                            this.$message({
                                type: 'error',
                                message: res.data.msg,
                                duration: 1000
                            })
                        }
                    })
                })
            },
            handleSelectionChange(val) {
                if (val.length > 0) {
                    this.checkedAll = true
                } else {
                    this.checkedAll = false
                }
                this.multipleSelection = val
            },
            closeDialog(formName) {
                this.dialogFormVisible = false;
                this.$refs[formName].resetFields()
            },
            //分页
            handleCurrentChange(val) {
                this.pageNo = val;
                this.getList();
            },
            //分页显示条数
            handleSizeChange(val) {
                this.size = val;
                this.getList();
            }
        }
    }
</script>

<style scoped>

</style>
